﻿using System;
using System.Configuration;
using System.Windows.Forms;
using System.Xml;

namespace ImageUploader
{
    public partial class Form_Config : Form
    {
        public Form_Config()
        {
            InitializeComponent();
        }

        private void Form_Config_Load(object sender, EventArgs e)
        {
            txt_ServerIP.Text = ConfigurationManager.AppSettings["Server"].ToString();
            txt_ServerPort.Text = ConfigurationManager.AppSettings["Port"].ToString();
            txt_Ftp.Text = ConfigurationManager.AppSettings["FtpServerIP"].ToString();
            txt_FtpUser.Text = ConfigurationManager.AppSettings["FtpUserID"].ToString();
            txt_FtpPwd.Text = ConfigurationManager.AppSettings["FtpPassword"].ToString();
            txt_Redis.Text = ConfigurationManager.AppSettings["Redis"].ToString();
            txt_Upgrade.Text = ConfigurationManager.AppSettings["Upgrade"].ToString();            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfa.AppSettings.Settings["Server"].Value = txt_ServerIP.Text;
                cfa.AppSettings.Settings["Port"].Value = txt_ServerPort.Text;
                cfa.AppSettings.Settings["FtpServerIP"].Value = txt_Ftp.Text;
                cfa.AppSettings.Settings["FtpUserID"].Value = txt_FtpUser.Text;
                cfa.AppSettings.Settings["FtpPassword"].Value = txt_FtpPwd.Text;
                cfa.AppSettings.Settings["Redis"].Value = txt_Redis.Text;
                cfa.AppSettings.Settings["Upgrade"].Value = txt_Upgrade.Text;
                cfa.Save();
                ConfigurationManager.RefreshSection("appSettings");
                MessageBox.Show("修改成功");
                this.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 更新配置文件信息
        /// </summary>
        /// <param name="name">配置文件字段名称</param>
        /// <param name="Xvalue">值</param>
        private void UpdateConfig(string name, string Xvalue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.ExecutablePath + ".config");
            XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
            XmlElement ele = (XmlElement)node;
            ele.SetAttribute("value", Xvalue);
            doc.Save(Application.ExecutablePath + ".config");
        }
    }
}
